package algorithmic_data_structure.堆与堆排序.加强堆;

import java.util.Arrays;

public class 删除被覆盖区间 {
    class Line{
        int left;
        int right;

        public Line(int left, int right) {
            this.left = left;
            this.right = right;
        }
    }
    public int removeCoveredIntervals(int[][] intervals) {
        int res = intervals.length;
        Line[] allLine = new Line[intervals.length];
        for (int i = 0; i < intervals.length; i++) {
            allLine[i] = new Line(intervals[i][0],intervals[i][1]);
        }
        Arrays.sort(allLine,(l1,l2) -> {
            //起点相同，就按终点从大到小pai
            return l1.left == l2.left ? l2.right-l1.right : l1.left - l2.left;
        });
        int maxSide = allLine[0].right;
        for (int i = 1; i < allLine.length; i++) {
            if (allLine[i].right <= maxSide){
                res--;
            }else{
                maxSide = allLine[i].right;
            }
        }
        return res;
    }
}
